<template>
  <div class='container'>
    <el-dialog
      title="新增文章"
      :visible="addDialogVisible"
      width="60%"
      @close='close'
      @open='open'
     >
    <el-form label-width="80px" :model="addForm" :rules="rules" ref="addFormRef">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="addForm.title"></el-input>
      </el-form-item>
    <el-form-item label="文章内容" prop="articleBody">
      <div class="middle">
      <quillEditor v-model="addForm.articleBody"></quillEditor>
      </div>
    </el-form-item>
     <el-form-item label="视频地址" prop="videoURL">
        <el-input v-model="addForm.videoURL"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="close">取 消</el-button>
    <el-button type="primary" @click="confirmAdd">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import { add, detail, update } from '@/api/hmmm/articles.js'
export default {
  name: 'ArticlesAdd',
  components: {
    quillEditor
  },
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false
    },
    editData: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data () {
    return {
      addForm: {
        title: '',
        articleBody: '',
        videoURL: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', triggle: ['blur', 'change'] }
        ],
        articleBody: [
          { required: true, message: '请输入文章标题', triggle: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    // 关闭添加文章对话框
    close () {
      this.$emit('update:addDialogVisible', false)
      // 重置数据
      this.addForm = {
        title: '',
        articleBody: '',
        videoURL: ''
      }
      this.$refs.addFormRef.resetFields()
    },
    // 开启编辑文章对话框
    async open () {
      if (this.editData.title) {
        const { data } = await detail(this.editData)
        this.addForm = data
        // console.log(data)
      }
    },
    // 添加文章
    confirmAdd () {
      this.$refs.addFormRef.validate(async boo => {
        if (boo) {
          // 编辑状态
          if (this.addForm.id) {
            const res = await update(this.addForm)
            if (res.status === 200) this.$message.success('编辑文章成功')
          } else {
            // 添加文章
            const res = await add(this.addForm)
            if (res.status === 200) this.$message.success('新增文章成功')
          }
          this.close()
          this.$emit('updateArticles')
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.middle{
  ::v-deep.ql-editor{
    height: 200px!important;
  }
}
</style>
